import PySimpleGUI as sg
from openpyxl import load_workbook


def _print_window() -> sg.Window:
    """
    标签打印的界面
    :return: 返回一个sg的window对象
    """
    biaoqian_layout = [
        [sg.Text('起始数值:', font="20px", pad=(10, 10)), sg.Input(size=(6, 4), key='Q-input'),
         sg.Text('年份选择:', font="20px"),
         sg.InputCombo([str(year) for year in range(2022, 2101)], size=(8, 1),
                       key='year_combo',
                       background_color="white", default_value='2022'),
         sg.Text('月份选择:', font="20px"),
         sg.InputCombo(['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'], size=(8, 1),
                       key='month_combo',
                       background_color="white", default_value='01'),
         sg.Text('生成数量:', font="20px"), sg.InputCombo([_ for _ in range(10, 110, 10)], size=(8, 1), key='number_combo', default_value=20)],
        [sg.FileBrowse(key='-file_path-', button_text="选择Excel文件", size=(16, 1), enable_events=True, target='input',
                       button_color=('Purple', 'white'), font="10px"),
         sg.InputText(key='input', size=(28, 2)),
         sg.Button(button_text='生成标签数据', size=(15, 1), font="10px", key='-update-',
                   button_color=('Purple', 'white'))],
    ]
    window = sg.Window('标签打印', biaoqian_layout, keep_on_top=True, finalize=True)
    return window


def write_to_excel(values: dict) -> str:
    """
    :param values:窗口页面的配置值
    :return: 返回打印标签的数据
    """
    last_label_begin = ''
    last_label_end = ''
    # 写入excel文件
    file_path = values['-file_path-']
    file = load_workbook(file_path)
    sheet = file['Sheet1']
    year = values['year_combo']
    month = values['month_combo']
    start_number = int(values['Q-input'])
    print_number = int(values['number_combo'])
    # 在1，3，5，7列写入条形码数据
    for col, number in zip(range(1, print_number * 2, 2), range(1, print_number + 1)):
        number_str = ''
        if (start_number + number) < 10:
            number_str = "00{}".format(start_number + number)
        elif 10 <= (start_number + number) < 100:
            number_str = "0{}".format(start_number + number)
        else:
            number_str = start_number + number
        sheet.cell(row=2, column=col).value = f"103{year[-2:]}{month}{number_str}"
    # 在2，4，6，8列写入标签数据
    for col, number in zip(range(2, print_number * 2 + 1, 2), range(1, print_number + 1)):
        if (start_number + number) < 10:
            number_str = "00{}".format(start_number + number)
        elif 10 <= (start_number + number) < 100:
            number_str = "0{}".format(start_number + number)
        else:
            number_str = start_number + number
        if number == min(range(1, print_number + 1)):
            last_label_begin = f"SN:103{year[-2:]}{month}{number_str}"
        if number == max(range(1, print_number + 1)):
            last_label_end = f"SN:103{year[-2:]}{month}{number_str}"
        sheet.cell(row=2, column=col).value = f"SN:103{year[-2:]}{month}{number_str}"
    file.save(file_path)
    # 上一次标签数据拼接
    last_label_data = "{}~{}".format(last_label_begin, last_label_end)
    return last_label_data
